
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "DRUID" or not module then return end
	
local ComboSkill = 35
local FinishSkill = 30
local SavageDefenseCost = 60

local L_OWLKIN_FRENZY = "Owlkin Frenzy"
local L_ECLIPSE = "Total Eclipse"
local L_MANGLE = "Mangle"
local L_MANGLE_CAT = "Mangle"
local L_MANGLE_BEAR = "Mangle"
local L_IMPROVED_MANGLE = "Improved Mangle"
local L_SWIFTMEND = "Swiftmend"
local L_LACERATE = "Lacerate"
local L_LIFEBLOOM = "Lifebloom"
local L_REJUVENATION = "Rejuvenation"
local L_REGENERATION = "Regeneration"
local L_STARSURGE = "Starsurge"
local L_SAVAGE_DEFENSE = "Savage Defense"

if GetLocale()=="koKR" then
L_OWLKIN_FRENZY = "올빼미야수의 광기"
L_ECLIPSE = "일월식"
L_MANGLE = "짓이기기"
L_MANGLE_CAT = "짓이기기"
L_MANGLE_BEAR = "짓이기기"
L_IMPROVED_MANGLE = "짓이기기 연마"
L_SWIFTMEND = "신속한 치유"
L_LACERATE = "가르기"
L_LIFEBLOOM = "피어나는 생명"
L_REJUVENATION = "회복"
L_REGENERATION = "재생"
L_STARSURGE = "별폭풍"
L_SAVAGE_DEFENSE = "야생의 방어"
end

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end

module.config = {
	[L_ECLIPSE] = {
		spellid = 79577
	},
	[L_MANGLE_CAT] = {
		spellid = 33876
	},
	[L_MANGLE_BEAR] = {
		spellid = 33878
	},
	[L_STARSURGE] = {
		spellid = 78674
	},
	[L_SWIFTMEND] = {
		spellid = 18562
	},
	[L_SAVAGE_DEFENSE] = {
		spellid = 62606
	},
}
function module:init()
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	self:update()
end

function module:update()
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		end
	end

	ComboSkill = 35
	if self.primary == 1 then
		self.config[L_STARSURGE].use = true
	elseif self.primary == 2 then
		self.config[L_MANGLE_CAT].use = true
	elseif self.primary == 3 then
		self.config[L_MANGLE_BEAR].use = true
	elseif self.primary == 4 then
		self.config[L_SWIFTMEND].use = true
	end

	if self.config[L_MANGLE_CAT].use then
		_, _, _, ComboSkill = GetSpellInfo(33876) -- Mangle for CAT
	end
	if self.config[L_SAVAGE_DEFENSE].use then
		_, _, _, SavageDefenseCost = GetSpellInfo(self.config[L_SAVAGE_DEFENSE].spellid) -- L_SAVAGE_DEFENSE
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:getComboText()
	local combo = GetComboPoints("player")
	local comboText, r, g, b = "", 1.0, 0.5, 0.1

	if( combo <= 0 )then
		return ""
	end

	if ( UnitPowerType("player") == 3 ) then -- energy
		local mana = UnitMana("player")
		if( mana >= ComboSkill )then
			r = 0.1
			g = 1.0
			b = 0.1 -- green
		elseif( mana >= FinishSkill )then
			r = 0.0
			g = 0.39
			b = 0.88 -- blue
		else
			r = 1.0
			g = 0.1
			b = 0.1 -- red
		end
	end
	return (":|cff%02x%02x%02x%d|r"):format(r * 255, g * 255, b * 255, combo)
end

function module:getSavageDefenseText()
	local r, g, b = 1.0, 0.5, 0.1
	if self.config[L_SAVAGE_DEFENSE].use then
		if ( UnitPowerType("player") == 1 ) then -- rage
			local charges = GetSpellCharges(self.config[L_SAVAGE_DEFENSE].spellid)
			if charges > 0 then
				local mana = UnitMana("player")
				if( mana >= SavageDefenseCost )then
					r = 0.1
					g = 1.0
					b = 0.1 -- green
				else
					r = 1.0
					g = 0.1
					b = 0.1 -- red
				end
				return (":|cff%02x%02x%02x%d|r"):format(r * 255, g * 255, b * 255, charges)
			end
		end
	end
	return ""
end
function module:getPlayerText()
	local text = ""
	local aura, rank, icon, count, debufType, duration, expirationTime
	local size = self.addon.db.fontSizeHealth

	if self.config[L_SAVAGE_DEFENSE].use then
		text = self:getSavageDefenseText()
		aura, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_SAVAGE_DEFENSE, nil, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
		return text
	end
	return text
end

function module:getTargetText()
	local text = ""
	local m_cat, m_bear, aura, rank, icon, count, debufType, duration, expirationTime
	local size = self.addon.db.fontSizeHealth

	-- 이거 확인해볼것.. mangle 기능이 바꼈는지!!??
	if self.config[L_MANGLE].use then
		local icon
		m_cat, _, icon, count, debufType, duration, expirationTime = UnitAura("target", L_MANGLE, nil, "HARMFUL")
		if m_cat then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = (":|T%s:%d|t"):format(icon, size)
		end
		if text == "" then
			m_bear, _, icon, count, debufType, duration, expirationTime = UnitAura("target", L_MANGLE, nil, "HARMFUL")
			if m_bear then
				size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
				text = (":|T%s:%d|t"):format(icon, size)
			end
		end
	end

	for i=1, GetNumShapeshiftForms() do
		local texture, name, isActive, isCastable = GetShapeshiftFormInfo(i)
		if(isActive) then
			if name == "표범 변신" then
				return self:getComboText() .. text
			elseif name == "곰 변신" then
				return self:getAuraCount("target", L_LACERATE, "HARMFUL") .. text
			end
		end
	end

	text = self:getAuraCount("target", L_LIFEBLOOM, "HELPFUL")
	if self.config[L_SWIFTMEND].use then
		aura, rank, icon, count, debufType, duration, expirationTime = UnitAura("target", L_REJUVENATION, nil, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
		aura, rank, icon, count, debufType, duration, expirationTime = UnitAura("target", L_REGENERATION, nil, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	if self.config[L_ECLIPSE].use then
		aura, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_ECLIPSE, nil, "HELPFUL")
		if aura then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end
	return text
end
